package pers.cocoadel.springcloud.service.impl;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import pers.cocoadel.springcloud.dao.StorageDao;
import pers.cocoadel.springcloud.domain.Storage;
import pers.cocoadel.springcloud.service.StorageService;

@Service
@Slf4j
public class StorageServiceImpl implements StorageService
{
    private final StorageDao storageDao;

    @Autowired(required = false)
    public StorageServiceImpl(StorageDao storageDao)
    {
        this.storageDao = storageDao;
    }

    @Transactional
    @Override
    public void decrease(Long productId, Integer count)
    {
        Storage storage = storageDao.getById(productId);
        int used = storage.getUsed() + count;
        int residue = storage.getResidue() - count;

        storageDao.update(productId, used, residue);
    }
}
